import Citys from "../config/citys";
import _ from "lodash";

export default {
    updateWeather(state, payload) {
        state.weartherInfo = payload.wearther
    },
    updatePostion(state, payload) {
        const {
            city
        } = payload.postion;
        const cityInfo = _.filter(Citys, (item) => {
            return city.indexOf(item.ReadmeName) > -1;
        });
        state.postion = cityInfo[0];
    },
    updateCityList(state) {
        let localCitys = JSON.parse(localStorage.getItem('CITY_LIST') || '[]');
        const cityList = _.concat(state.postion, localCitys);
        state.cityList = cityList;
    },
    addCity(state, payload) {
        const citys = JSON.parse(localStorage.getItem('CITY_LIST') || '[]');
        localStorage.setItem('CITY_LIST', JSON.stringify(_.concat(citys, payload)))
        const cityList = _.concat(state.cityList, payload);
        state.cityList = cityList;
    },
    deleteCity(state, payload) {
        const citys = JSON.parse(localStorage.getItem('CITY_LIST') || '[]');
        localStorage.setItem('CITY_LIST', JSON.stringify(_.filter(citys, item => {
            return item.id !== payload.id;
        })))
        const cityList = _.filter(state.cityList, item => {
            return item.id !== payload.id;
        })
        state.cityList = cityList;
    }
}